package com.kuizii.auth.shiro;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.kuizii.auth.util.AuthUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;



@Slf4j
public class JWTCredentialsMatcher implements CredentialsMatcher {
    /**
     * Matcher中直接调用工具包中的verify方法即可
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        String token = (String) authenticationToken.getCredentials();

        try {

            AuthUtils.verify(token);
            return true;
        } catch (JWTVerificationException e) {
            log.error("Token Error:{}", e.getMessage());
        } catch (Exception e) {
            log.error("Shiro 身份验证error", e);
        }
        return false;
    }
}